#define _CRT_SECURE_NO_WARNINGS 1
#include  "stack.h"
void StackInit(Stack* ps) {
	assert(ps);
	ps->size = 0;
	ps->top = NULL;
}
void StackPush(Stack* ps, STDataType x) {
	assert(ps);
	STNode*newnode= (STNode*)malloc(sizeof(STNode));
	assert(newnode);
	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	if (ps->top == NULL)ps->top = newnode;
	else {
		ps->top->next = newnode;
		newnode->prev = ps->top;
		ps->top = newnode;
	}
	ps->size++;
}
void StackPop(Stack* ps) {
	assert(ps&&ps->top);
	if (ps->top->prev== NULL) {
		free(ps->top);
		ps->top = NULL;
	}
	else {
		STNode* prev = ps->top->prev;
		free(ps->top); 
		ps->top = prev;
	}
	ps->size--;
}
STDataType StackTop(Stack* ps) {
	assert(ps && ps->top);
	return ps->top->data;
}
bool StackEmpty(Stack* ps){
	assert(ps);
	return ps->top == NULL;
}
int StackSize(Stack* ps) {
	return ps->size;
}
void StackDestroy(Stack* ps) {
	assert(ps);
	while (ps->top) {
		StackPop(ps);
	}
}